/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-08-23
 * Time: 18:59
 */

import com.beust.ah.A;
import jdk.management.resource.internal.inst.RandomAccessFileRMHooks;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.*;

/**
 * 飞鸟论坛测试用例
 */

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumCases extends InitAndEnd {

    private String email;

    /**
     * 正确的账号密码登陆
     *
     * @param username  用户名
     * @param password  密码
     * @param index_url 登陆页面地址
     */
    @Order(1)
    @ParameterizedTest
    //@CsvFileSource(resources = "login.csv")
    @CsvSource({"admin, admin, http://124.221.91.27:8888/index.html"})
    void LoginSuccess(String username, String password, String index_url) throws InterruptedException {
        // 1. 打开登陆网址
        webDriver.get("http://124.221.91.27:8888/sign-in.html");
        webDriver.manage().window().maximize();
        // 2. 输入账号
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        // 3. 输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        // 4. 点击登陆
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(300);
//        webDriver.manage().timeouts().implicitlyWait(Duration.ofDays(1));
        // 5. 确认 url 是否跳转
        String url = webDriver.getCurrentUrl();
        Assertions.assertEquals(index_url, url);

    }


    /**
     * 异常登陆
     *
     * @param username 用户名
     * @param password 密码
     */
    @Disabled
    @CsvFileSource(resources = "loginFailed.csv")
    @ParameterizedTest
    void loginFailed(String username, String password) {
        // 1. 打开登陆网址
        webDriver.get("http://124.221.91.27:8888/sign-in.html");
        webDriver.manage().window().maximize();
        // 2. 输入账号
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        // 3. 输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        // 4. 点击登陆
        webDriver.findElement(By.cssSelector("#submit")).click();

        // 5. 获取警告框
        WebElement waring = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        Assertions.assertEquals("×\n" +
                "警告\n" +
                "⽤户名或密码错误.", waring.getText());
    }


    /**
     * 检验首页文章列表是否正确
     */
    @Test
    @Order(2)
    void getArticleAll() throws InterruptedException {
//        webDriver.get("http://124.221.91.27:8888/index.html");
        sleep(3000);
        String board = webDriver.findElement(By.cssSelector("#nav_board_index > a > span.nav-link-title")).getText();
        Assertions.assertEquals("首页", board);
        sleep(1000);
        int count = webDriver.findElements(By.cssSelector(".article_list_a_title")).size();
        System.out.println("count :" + count);
        Assertions.assertNotEquals(0, count);
    }


    /**
     * 检查导航栏
     */
    @Order(3)
    @Test
    void checkNavigation() {
        webDriver.manage().window().maximize();
        webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        // 检验版块名
        String board1 = webDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a/span[2]")).getText();
        Assertions.assertEquals("Java", board1);

        String board2 = webDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(3) > a > span.nav-link-title")).getText();
        Assertions.assertEquals("C++", board2);

        String board3 = webDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(4) > a > span.nav-link-title")).getText();
        Assertions.assertEquals("前端技术", board3);

        String board4 = webDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(5) > a > span.nav-link-title")).getText();
        Assertions.assertEquals("MySQL", board4);

        String board5 = webDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(6) > a > span.nav-link-title")).getText();
        Assertions.assertEquals("面试宝典", board5);

        String board6 = webDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(7) > a > span.nav-link-title")).getText();
        Assertions.assertEquals("经验分享", board6);

        String board7 = webDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(8) > a > span.nav-link-title")).getText();
        Assertions.assertEquals("招聘信息", board7);

        String board8 = webDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(9) > a > span.nav-link-title")).getText();
        Assertions.assertEquals("福利待遇", board8);

        String board9 = webDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(10) > a > span.nav-link-title")).getText();
        Assertions.assertEquals("灌水区", board9);
    }


    /**
     * 检验制定板块下的文章数目
     */
    @Order(4)
    @Test
    void checkBoardOfArticle() throws InterruptedException {
        // 1. 点击 Java 版块
        webDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a/span[2]")).click();
        sleep(3000); // 保证加载板块下的全部帖子和页面

        // 1.1 检验该板块下的名称
        String board1 = webDriver.findElement(By.xpath("/html/body/div[1]/div/div[1]/div[1]/div/div/div[1]/h2")).getText();
        Assertions.assertEquals("Java", board1);

        // 1.2 检验该板块下的帖子数
        String num1 = webDriver.findElement(By.cssSelector("#article_list_count_board")).getText();

        // 获取帖子标题数量
        sleep(1000);
        int ArticleCount1 = webDriver.findElements(By.cssSelector(".article_list_a_title")).size();
        Assertions.assertEquals(num1, "帖子数量 : " + ArticleCount1);


        // 2. 点击招聘信息板块
        webDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[8]/a/span[2]")).click();
        sleep(3000); // 确保版块下文章的加载和页面加载

        // 2.1 检验该板块下的名称
        String board2 = webDriver.findElement(By.xpath("//*[@id=\"article_list_board_title\"]")).getText();
        Assertions.assertEquals("招聘信息", board2);

        // 2.2 检验版块下的帖子数量
        String num2 = webDriver.findElement(By.cssSelector("#article_list_count_board")).getText();
        Assertions.assertEquals("帖子数量 : 0", num2);

        // 2.3 检验板块下没有文章
        String checkArticle = webDriver.findElement(By.cssSelector("#artical-items-body")).getText();
        Assertions.assertEquals("还没有帖子", checkArticle);
    }


    /**
     * 检验首页第一篇帖子详情内容 -- 检验作者自己的帖子
     */
    @Test
    @Order(5)
    void articleDetail() throws InterruptedException {
        webDriver.manage().window().maximize();
        // 1. 点击回到首页
        webDriver.findElement(By.cssSelector("#nav_board_index > a > span.nav-link-title")).click();
        webDriver.manage().timeouts().implicitlyWait(Duration.ofDays(1));
        webDriver.manage().window().maximize();
        // 2. 确认到达首页
        String boardName = webDriver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("首页", boardName);

        // 3. 点击第一篇文章
        webDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        sleep(3000);
        // 4. 确认帖子
        // 4.1 确认帖子标题
        String title = webDriver.findElement(By.cssSelector("#details_article_title")).getText();
        Assertions.assertEquals("测试", title);
        // 4.2 确认帖子发布时间
        String createTIme = webDriver.findElement(By.cssSelector("#details_article_createTime")).getText();
        Assertions.assertEquals("2023-08-16 09:26:16", createTIme);
        // 4.3 确认帖子浏览数 -- 一直在变, 可以不进行确认
//        String visitCount = webDriver.findElement(By.cssSelector("#details_article_visitCount")).getText();
//        Assertions.assertEquals("4", visitCount);
        // 4.4 确认帖子点赞数
//        String likeCount = webDriver.findElement(By.cssSelector("#details_article_likeCount")).getText();
//        Assertions.assertEquals("6", likeCount);

        // 5. 测试用户发布的帖子
        // 5.1 帖子的发布者
        String author = webDriver.findElement(By.cssSelector("#article_details_author_name")).getText();
        sleep(3000);
        String name = webDriver.findElement(By.cssSelector("#index_nav_nickname")).getText();
        Assertions.assertEquals(name, author);
        // 5.2 帖子的标题
        String articleTitle = webDriver.findElement(By.cssSelector("#details_article_content_title")).getText();
        Assertions.assertEquals("测试", articleTitle);
        // 5.3 帖子的正文
        String content = webDriver.findElement(By.cssSelector("#details_article_content > p")).getText();
        Assertions.assertEquals("测试一下", content);
    }


    /**
     * 测试回复帖子功能
     */
    @Disabled
    @Order(6)
    @Test
    void replyArticle() throws InterruptedException {
        // 全屏
        webDriver.manage().window().maximize();
        sleep(1000);
        // 滚动滚动条到底部
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("document.documentElement.scrollTop = 10000");
        sleep(1000);
        // 创建双击时间
        Actions action = new Actions(webDriver);
        // 双击回复窗口
        action.doubleClick(webDriver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span"))).perform();
        sleep(1000);
//        WebElement element = webDriver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));
//        if (element.isEnabled() && element.isDisplayed()) {
//            // 执行与元素的交互操作
//            element.click();
//        } else {
//            // 处理不可交互元素的情况
//            System.out.println("element 元素不可用或不可见");
//        }
//
//        WebElement element1 = webDriver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span"));
//        if (element1.isEnabled() && element1.isDisplayed()) {
//            // 执行与元素的交互操作
//            element1.click();
//        } else {
//            // 处理不可交互元素的情况
//            System.out.println("element1 元素不可用或不可见");
//        }
//
//        WebElement element2 = webDriver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span"));
//        if (element2.isEnabled() && element2.isDisplayed()) {
//            // 执行与元素的交互操作
//            element2.click();
//        } else {
//            // 处理不可交互元素的情况
//            System.out.println("element2 元素不可用或不可见");
//        }
//
//        WebElement element3 = webDriver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));
//        if (element3.isEnabled() && element3.isDisplayed()) {
//            // 执行与元素的交互操作
//            element3.click();
//        } else {
//            // 处理不可交互元素的情况
//            System.out.println("element3 元素不可用或不可见");
//        }
//
//        WebElement element4 = webDriver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > div.CodeMirror-activeline-background.CodeMirror-linebackground"));
//        if (element4.isEnabled() && element4.isDisplayed()) {
//            // 执行与元素的交互操作
//            element4.click();
//        } else {
//            // 处理不可交互元素的情况
//            System.out.println("element4 元素不可用或不可见");
//        }


        // 输入内容
        webDriver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code")).sendKeys("你好");
        // 点击回复
        webDriver.findElement(By.cssSelector("#details_btn_article_reply > span")).click();
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        Assertions.assertEquals(
                "成功\n" +
                        "回复成功", success.getText());
    }


    /**
     * 测试点赞功能 - 点击点赞后, 获取点赞数目
     */
    @Test
    @Order(7)
    void checkLikeCount() throws InterruptedException {
        webDriver.manage().window().maximize();

        webDriver.findElement(By.cssSelector("#details_btn_like_count")).click();
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        Assertions.assertEquals("×\n" +
                "成功\n" +
                "点赞成功", success.getText());
        sleep(5000);

    }


    /**
     * 检查帖子下面的回复帖子的正文和回复人以及回复时间
     */
    @Test
    @Order(8)
    void checkReplyArticle() {

        // 4.5 确认帖子回复术 -- 回复帖子后需要更新
//        String replyCount = webDriver.findElement(By.cssSelector("#details_article_replyCount")).getText();
//        Assertions.assertEquals("1", replyCount);
        webDriver.manage().window().maximize();

        // 1. 确认回复人
        String replyAuthor = webDriver.findElement(By.cssSelector("#details_reply_area > div > div.col-3.card > div > h3 > a")).getText();
        Assertions.assertEquals("testUser", replyAuthor);

        // 2. 确认回复内容
        String replyContent = webDriver.findElement(By.cssSelector("#details_article_reply_content_15 > p")).getText();
        Assertions.assertEquals("你好", replyContent);

        // 3. 确认回复时间
        String replyCreteTime = webDriver.findElement(By.cssSelector("#details_reply_area > div > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto > div > div > ul > li")).getText();
        //String replyCreteTime = webDriver.findElement(By.xpath("//*[@id=\"details_reply_area\"]/div/div[2]/div[2]/div/div/ul/li/text()")).getText();
        Assertions.assertEquals("2023-08-25 10:38:52", replyCreteTime);
    }


    /**
     * 检验私信功能
     */
    @Test
    @Order(9)
    @Disabled
    void checkMessage() throws InterruptedException {
        // 全屏
        webDriver.manage().window().maximize();
        sleep(1000);
        // 滚动滚动条到底部
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("document.documentElement.scrollTop = 10000");
        sleep(1000);

        // 1. 点击帖子作者发私信
        webDriver.findElement(By.cssSelector("#details_reply_area > div > div.col-3.card > div > div > a")).click();
        webDriver.manage().timeouts().implicitlyWait(Duration.ofDays(1));

        // 2. 发送消息
        webDriver.findElement(By.cssSelector("#index_message_receive_content")).sendKeys("我给你发私信了");

        // 3. 点击发送
        webDriver.findElement(By.cssSelector("#btn_index_send_message")).click();
        sleep(1000);
        // 4. 确认发送成功
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        Assertions.assertEquals("×\n" +
                "成功\n" +
                "发送成功", success.getText());

        webDriver.findElement(By.cssSelector("#index_message_modal > div > div > div.modal-header > button")).click();
//        sleep(5000);
    }


    /**
     * 路径一 : 从帖子详情点击用户名进入到该用户的详情
     */
    @Test
    @Order(10)
    void checkPerson1() throws InterruptedException {

        webDriver.manage().window().maximize();
        sleep(1000);
        // 滚动滚动条到底部
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("document.documentElement.scrollTop = 0");
        sleep(1000);

        String username = webDriver.findElement(By.cssSelector("#article_details_author_name")).getText();

        // 1. 点击当前用户名称进入
        webDriver.findElement(By.cssSelector("#article_details_author_name")).click();
        sleep(1000);
        // 2. 检验名称是否和之前进入时一致
        String curUsername = webDriver.findElement(By.cssSelector("#profile_nickname")).getText();
        Assertions.assertEquals(username, curUsername);
        // 3. 检验发帖数
        String articleCount = webDriver.findElement(By.cssSelector("#profile_articleCount")).getText();
        // 3.1 获取帖子总数
        int size = webDriver.findElements(By.cssSelector(".profile_article_list_a_title")).size();
        Assertions.assertEquals(articleCount + "", size + "");
        // 4. 检验邮箱 -- 后续会修改
//        String email = webDriver.findElement(By.cssSelector("#profile_email")).getText();
//        Assertions.assertEquals("zjingyin0524@163.com", email);
        email = webDriver.findElement(By.cssSelector("#profile_email")).getText();
        // 5. 检验注册日期
        String createTime = webDriver.findElement(By.cssSelector("#profile_createTime")).getText();
        Assertions.assertEquals("2023-08-14 12:26:57", createTime);
        // 6. 检验个人简介 -- 后续会更改
//        String remark = webDriver.findElement(By.cssSelector("#profile_remark")).getText();
//        Assertions.assertEquals("这个人非常的懒 ~ 什么也没有留下", remark);
    }


    /**
     * 测试禁止给自己发私信
     */
    @Order(11)
    @Test
    void checkFailedMessage() throws InterruptedException {
        webDriver.manage().window().maximize();

        // 1. 点击我的帖子中的发送私信
        webDriver.findElement(By.cssSelector("#btn_profile_send_message")).click();

        // 2. 输入发送内容
        webDriver.findElement(By.cssSelector("#index_message_receive_content")).sendKeys("禁止发送私信给自己");

        // 3. 点击发送
        webDriver.findElement(By.cssSelector("#btn_index_send_message")).click();
        sleep(1000);

        // 4. 获取 toast 弹窗
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        Assertions.assertEquals("×\n" +
                "警告\n" +
                "code = 1000, message = 操作失败 无法给自己发送站内信", success.getText());

        // 5. 关闭私信框
        webDriver.findElement(By.cssSelector("#index_message_modal > div > div > div.modal-header > button")).click();

    }


    /**
     * 入口二 : 从头像处下拉框选择我的帖子进入
     */
    @Test
    @Order(12)
    void checkMenuPerson2() throws InterruptedException {
        webDriver.manage().window().maximize();

        // 1. 点击头像框
        webDriver.findElement(By.cssSelector("#index_nav_avatar")).click();
        // 2. 点击我的帖子
        webDriver.findElement(By.cssSelector("#index_user_profile")).click();
        sleep(1000);
        // 3. 确定是否为当前登录用户的帖子中心
        String username = webDriver.findElement(By.cssSelector("#profile_nickname")).getText();
        String name = webDriver.findElement(By.cssSelector("#index_nav_nickname")).getText();
        Assertions.assertEquals(name, username);
        // 4. 检验发帖数
        String articleCount = webDriver.findElement(By.cssSelector("#profile_articleCount")).getText();
        // 4.1 获取帖子总数
        int size = webDriver.findElements(By.cssSelector(".profile_article_list_a_title")).size();
        Assertions.assertEquals(articleCount + "", size + "");
        // 5. 检验邮箱 -- 后续会修改
//        String email = webDriver.findElement(By.cssSelector("#profile_email")).getText();
//        Assertions.assertEquals("zjingyin0524@163.com", email);
        // 6. 检验注册日期
        String createTime = webDriver.findElement(By.cssSelector("#profile_createTime")).getText();
        Assertions.assertEquals("2023-08-14 12:26:57", createTime);
        // 7. 检验个人简介
//        String remark = webDriver.findElement(By.cssSelector("#profile_remark")).getText();
//        Assertions.assertEquals("这个人非常的懒 ~ 什么也没有留下", remark);
    }


    /**
     * 检验头像进入个人中心
     */
    @Test
    @Order(13)
    void checkPersonCenter() throws InterruptedException {
        webDriver.manage().window().maximize();

        // 提前记录头像入口处进入我的帖子时的用户昵称
        String username = webDriver.findElement(By.cssSelector("#profile_nickname")).getText();
        // 1. 点击头像下拉框
        webDriver.findElement(By.cssSelector("#index_nav_avatar")).click();
        // 2. 点击个人中心
        webDriver.findElement(By.cssSelector("#index_user_settings")).click();
        sleep(1000);
        // 3. 检验初始昵称
        // 注意为 input 标签文本, 需要获取 value 属性
        String curUsername = webDriver.findElement(By.xpath("//*[@id=\"setting_input_nickname\"]")).getAttribute("value");
        Assertions.assertEquals(username, curUsername);
    }


    /**
     * 错误的修改昵称 -- 直接点击
     */
    @Test
    @Order(14)
    void checkFailedModifyUsername() throws InterruptedException {
        webDriver.manage().window().maximize();

        // 1. 直接点击修改昵称
        webDriver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        sleep(1000);
        // 2. 检验弹窗

        WebElement warming = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        Assertions.assertEquals("×\n" +
                "警告\n" +
                "请输入修改的内容", warming.getText());
        sleep(5000);
    }


    /**
     * 检验个人中心正确修改昵称
     */
    @Test
    @Order(15)
    void checkSuccessModifyUsername() throws InterruptedException {
        webDriver.manage().window().maximize();

        Random random = new Random();
        // 1. 生成随机昵称
        String modifyUsername = "别时" + random.nextInt(11) + "须清欢" + random.nextInt(11);

        // 注意清空输入框原本昵称
        webDriver.findElement(By.xpath("//*[@id=\"setting_input_nickname\"]")).clear();

        // 2. 输入昵称
        webDriver.findElement(By.xpath("//*[@id=\"setting_input_nickname\"]")).sendKeys(modifyUsername);

        // 3. 点击修改
        webDriver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        sleep(1000);

        // 4. 确认修改成功
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div:nth-child(1)")));

        Assertions.assertEquals("×\n" +
                "成功\n" +
                "修改成功", success.getText());
//        sleep(4000);
        webDriver.manage().timeouts().implicitlyWait(Duration.ofDays(1));
        // 5. 确认修改后的名称
        String curUsername = webDriver.findElement(By.cssSelector("#settings_nickname")).getText();
        Assertions.assertEquals(modifyUsername, curUsername);
    }


    /**
     * 检验错误修改个人中心邮箱
     */
    @Test
    @Order(16)
    void checkFailedModifyEmail() throws InterruptedException {
        webDriver.manage().window().maximize();
        // 1. 点击修改
        webDriver.findElement(By.cssSelector("#setting_submit_email")).click();
        sleep(1000);
        WebElement warming = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div:nth-child(2)")));
        Assertions.assertEquals("×\n" +
                "警告\n" +
                "请输入修改的内容", warming.getText());
        sleep(5000); // TODO : 后续弹窗多次跳出, 会遮挡修改按钮
    }


    /**
     * 检验成正确修改个人中心邮箱
     */
    @Test
    @Order(17)
    void checkSuccessModifyEmail() throws InterruptedException {
        webDriver.manage().window().maximize();
        // 1. 清空输入框
        webDriver.findElement(By.cssSelector("#setting_input_email")).clear();
        // 2. 输入内容
        Random random = new Random();
        String newEmail = "zjy" + random.nextInt(10) + "@" + random.nextInt(10) + ".com";
        webDriver.findElement(By.cssSelector("#setting_input_email")).sendKeys(newEmail);
        // 3. 点击提交
        webDriver.findElement(By.cssSelector("#setting_submit_email")).click();
        sleep(1000);
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div:nth-child(1)")));
        Assertions.assertEquals("×\n" +
                "成功\n" +
                "修改成功", success.getText());
        String curEmail = webDriver.findElement(By.cssSelector("#setting_input_email")).getAttribute("value");
        Assertions.assertEquals(newEmail, curEmail);
    }


    /**
     * 检验错误的修改个人中心手机号
     */
    @Test
    @Order(18)
    void checkFailedModifyPhone() throws InterruptedException {
        // 滑动窗口至底部
        webDriver.manage().window().maximize();
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("document.documentElement.scrollTop = 5000");
        sleep(1000);
        // 1. 点击修改
        webDriver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
        // 2. 获取警告弹窗
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div:nth-child(2)")));
        Assertions.assertEquals("×\n" +
                "警告\n" +
                "请输入修改的内容", success.getText());

    }


    /**
     * 检验正确的个人中心修改手机号
     */
    @Test
    @Order(19)
    void checkSuccessModifyPhone() throws InterruptedException {
        // 1. 清空输入框
        webDriver.findElement(By.cssSelector("#setting_input_phoneNum")).clear();
        // 2. 输入号码
        Random random = new Random();
        String phone = " 123" + random.nextInt(100) + "45" + random.nextInt(101);
        webDriver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys(phone);
        // 3. 点击修改
        webDriver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
        // 4. 获取成功弹窗
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div:nth-child(3)")));
        Assertions.assertEquals("×\n" +
                "成功\n" +
                "修改成功", success.getText());
        sleep(5000); // 等待当前洁面霜最后一个 toast 提示框结束, 防止后续提示框遮挡界面元素
    }


    /**
     * 检查错误的修改个人中心简介
     */
    @Test
    @Order(20)
    void checkFailedModifyRemark() throws InterruptedException {
        // 1. 直接点击修改
        webDriver.findElement(By.cssSelector("#settings_submit_remark")).click();
        // 2. 获取警告弹窗
        sleep(1000);
        WebElement warming = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div:nth-child(1)")));
        Assertions.assertEquals("×\n" +
                "警告\n" +
                "请输入修改的内容", warming.getText());
    }


    @Test
    @Order(21)
    void checkSuccessModifyRemark() throws InterruptedException {
        // 1. 清空简介框
        webDriver.findElement(By.cssSelector("#settings_textarea_remark")).clear();
        // 2. 输入内容
        Random random = new Random();
        String remark = "这个人很懒 ~ " + random.nextInt(11) + " 版本 " + random.nextInt(101);
        webDriver.findElement(By.cssSelector("#settings_textarea_remark")).sendKeys(remark);
        // 3. 点击修改
        webDriver.findElement(By.cssSelector("#settings_submit_remark")).click();
        // 4. 获取第二个 toast 弹窗
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div:nth-child(2)")));
        Assertions.assertEquals("×\n" +
                "成功\n" +
                "修改成功", success.getText());
        sleep(3000);
    }


    /**
     * 点击小铃铛, 检验站内信
     */
    @Test
    @Order(22)
    void checkMail() throws InterruptedException {
        // 1. 滚到到页面顶端
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("document.documentElement.scrollTop = 0");
        sleep(1000);
        // 2. 点击小铃铛
        webDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg")).click();
        sleep(1000);
        // 3. 获取小铃铛内第一条消息
        // 3.1 消息状态 -- 后续点击以后会变动

        // 3.2 发送人
        String postUser = webDriver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a > span:nth-child(2) > strong")).getText();
        Assertions.assertEquals("testUser", postUser);
        // 3.3 正文
        String content = webDriver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > div")).getText();
        Assertions.assertEquals("?", content);
    }


    /**
     * 针对第一条未读的站内信回复消息
     */
    @Test
    @Order(23)
    void checkReplyMail() throws InterruptedException {
        // 1. 点击第一条未回复的站内信
        webDriver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a > span:nth-child(2)")).click();
        // 2. 检验该条站内信内容
        sleep(1000);
        webDriver.findElement(By.cssSelector("#index_message_detail_content")).getAttribute("value");
        // 3. 点击回复
        webDriver.findElement(By.cssSelector("#btn_index_message_reply")).click();
        sleep(1000);
        // 4. 回复内容
        String message = "我回复你了";
        webDriver.findElement(By.cssSelector("#index_message_reply_receive_content")).sendKeys(message);
        // 5. 点击回复
        webDriver.findElement(By.cssSelector("#btn_index_send_message_reply")).click();
        sleep(1000);
        // 6. 检验
        WebElement success = new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(webDriver -> webDriver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        Assertions.assertEquals("×\n" +
                "成功\n" +
                "回复成功", success.getText());

        // 7. 关闭站内信
        webDriver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button")).click();
    }


    /**
     * 针对作者自己的文章进行编辑
     */
    @Test
    @Order(24)
    @Disabled
    void checkEdit() {
        // 1. 关闭站内信
        webDriver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button")).click();
    }


    /**
     * 检验退出
     */
    @Test
    @Order(25)
    void logOut() {
        // 1. 点击头像
        webDriver.findElement(By.cssSelector("#index_nav_avatar")).click();
        // 2. 点击退出
        webDriver.findElement(By.cssSelector("#index_user_logout > span")).click();
        // 3. 校验url
        // 3.1 获取跳转后的 URL
        webDriver.manage().timeouts().implicitlyWait(Duration.ofDays(1));
        String url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://124.221.91.27:8888/index.html", url);
    }

    @Test
    @Order(26)
    void register() throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(Duration.ofDays(1));
        webDriver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a")).click();
        sleep(1000);
        // 1. 输入用户名
        webDriver.findElement(By.cssSelector("#username")).sendKeys("test");
        // 2. 输入昵称
        webDriver.findElement(By.cssSelector("#nickname")).sendKeys("test");
        // 3. 输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        // 4. 确认密码
        webDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("123");
        // 5. 勾选同意
        webDriver.findElement(By.cssSelector("#policy")).click();
        // 6. 点击注册
        webDriver.findElement(By.cssSelector("#submit")).click();
        // 7. 跳转至首页
        sleep(100);
        Assertions.assertNotEquals(webDriver.getCurrentUrl(), "http://124.221.91.27:8888/sign-up.html");
    }
}
